package com.struts.deal.news;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.struts.web.news.ViewNewsForm;
import com.ibatis.po.system.TNews;
import com.lg.system.CSqlMap;
import com.lg.system.CTurnPage;

public class Deal {

	/**
	 * �������
	 * @param request
	 * @param aWebForm
	 * @throws Exception
	 */
	public static void viewNews(HttpServletRequest request,ViewNewsForm aWebForm) throws Exception{
		String id=request.getParameter("ID");
		TNews news = new TNews();
		TNews news1 = new TNews();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		news.setId(id);
		try{
			SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
			news=(TNews)sqlmap.queryForObject("T_NEWS.abatorgenerated_selectByPrimaryKey",news);
			aWebForm.setTitle(news.getTitle());
			aWebForm.setContent(news.getContent());
			aWebForm.setInfoSource(news.getInfoSource());
			aWebForm.setIssueTime(format.format(news.getIssueDate()));
			aWebForm.setViewedTimes(news.getViewTime());
			
			//设置新闻浏览次数
			String viewTime=(Integer.parseInt(news.getViewTime())+1)+"";
			news1.setId(id);
			news1.setViewTime(viewTime);
			
			sqlmap.update("T_NEWS.abatorgenerated_updateByPrimaryKeySelective",news1);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
         
		}
	}

	/**
	 * 最新动态
	 * @param request
	 */
	public static void queryNewsByKindNews(HttpServletRequest request){
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TNews news = new TNews();
		String newsTitle = "";
		CTurnPage page = new CTurnPage();
		try{
			String newsKind = request.getParameter("newsKind");
			news.setKindOfNewsId(newsKind);
			page.setQueryList("T_NEWS.selectAllByNewsSort",news);
			page.setPerPageRows(10);//设置每页行数

			newsTitle = (String)sqlmap.queryForObject("T_NEWS.selectByNewsTitle",news);
			page.setTableName(newsTitle);
			page.excute(request);
			request.setAttribute("page", page);
			getLawList(request);
			getMediaList(request);
			getHopSearchList(request);
		}catch(Exception e){
			e.printStackTrace();
		}

	}
	
	/**
	 * 政策法规
	 * @param request
	 */
	public static void queryNewsByKindLaw(HttpServletRequest request){
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TNews news = new TNews();
		String newsTitle = "";
		CTurnPage page = new CTurnPage();
		try{
			String newsKind = request.getParameter("newsKind");
			news.setKindOfNewsId(newsKind);
			page.setQueryList("T_NEWS.selectByNewsSort",news);
			page.setPerPageRows(10);//设置每页行数

			newsTitle = (String)sqlmap.queryForObject("T_NEWS.selectByNewsTitle",news);
			page.setTableName(newsTitle);
			page.excute(request);
			request.setAttribute("page", page);
			getNewsList(request);
			getMediaList(request);
			getHopSearchList(request);
		}catch(Exception e){
			e.printStackTrace();
		}

	}
	
	/**
	 * 企业推荐
	 * @param request
	 */
	public static void queryNewsByKindCorpor(HttpServletRequest request){
		CTurnPage page = new CTurnPage();
		try{
			page.setQueryList("T_CORPORATION.selectAllRecommend");
			page.setPerPageRows(10);//设置每页行数

			page.setTableName("企业推荐");
			page.excute(request);
			request.setAttribute("page", page);
			getLawList(request);
			getMediaList(request);
			getHopSearchList(request);
		}catch(Exception e){
			e.printStackTrace();
		}

	}
	
	
	/**
	 * 传媒博览
	 * @param request
	 */
	public static void queryNewsByKindMedia(HttpServletRequest request){
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TNews news = new TNews();
		String newsTitle = "";
		CTurnPage page = new CTurnPage();
		try{
			String newsKind = request.getParameter("newsKind");
			news.setKindOfNewsId(newsKind);
			page.setQueryList("T_NEWS.selectByNewsSort",news);
			page.setPerPageRows(10);//设置每页行数

			newsTitle = (String)sqlmap.queryForObject("T_NEWS.selectByNewsTitle",news);
			page.setTableName(newsTitle);
			page.excute(request);
			request.setAttribute("page", page);
			getLawList(request);
			getNewsList(request);
			getHopSearchList(request);
		}catch(Exception e){
			e.printStackTrace();
		}

	}

    /**
     * 获取传媒博览
     * @param request
     */
	public static void getMediaList(HttpServletRequest request){
		TNews tnews = new TNews();
		List mediaList = null;//2传媒博览
		SqlMapClient sqlmap=CSqlMap.getSqlMapConfig();
		try{
			tnews.setKindOfNewsId("2");
			mediaList=sqlmap.queryForList("T_NEWS.selectByNewsSort",tnews);
			if(mediaList!=null){
				for(int i=0;i<mediaList.size();i++){
					TNews tn = (TNews)mediaList.get(i);
					String str = tn.getTitle();
					if(str.length()>20){
						tn.setTitle(str.substring(0, 19)+"...");
						mediaList.set(i, tn);
					}
				}
				request.setAttribute("medialist", mediaList);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
    /**
     * 新闻动态
     * @param request
     */
	public static void getNewsList(HttpServletRequest request){
		TNews tnews = new TNews();
		List hopNewsList = null;//0最新动态
		SqlMapClient sqlmap=CSqlMap.getSqlMapConfig();
		try{
			tnews.setKindOfNewsId("0");
			hopNewsList=sqlmap.queryForList("T_NEWS.selectByNewsSort",tnews);
			if(hopNewsList!=null){
				for(int i=0;i<hopNewsList.size();i++){
					TNews tn = (TNews)hopNewsList.get(i);
					String str = tn.getTitle();
					if(str.length()>20){
						tn.setTitle(str.substring(0, 19)+"...");
						hopNewsList.set(i, tn);
					}
				}
				request.setAttribute("hopnewslist", hopNewsList);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 政策法规
	 * @param request
	 */
	public static void getLawList(HttpServletRequest request){
		TNews tnews = new TNews();
		List lawList = null;//3政策法规
		SqlMapClient sqlmap=CSqlMap.getSqlMapConfig();
		try{
			tnews.setKindOfNewsId("3");
			lawList=sqlmap.queryForList("T_NEWS.selectByNewsSort",tnews);
			if(lawList!=null){
				System.out.println("ddddd:"+lawList.size());
				for(int i=0;i<lawList.size();i++){
					TNews tn = (TNews)lawList.get(i);
					String str = tn.getTitle();
					if(str.length()>20){
						tn.setTitle(str.substring(0, 19)+"...");
						lawList.set(i, tn);
					}
				}
				request.setAttribute("lawlist", lawList);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取热门搜索
	 * @param request
	 */
	public static void getHopSearchList(HttpServletRequest request){
		List hopSearchList = null;//6热门搜索
		SqlMapClient sqlmap=CSqlMap.getSqlMapConfig();
		try{
			System.out.println("here");
			hopSearchList = sqlmap.queryForList("T_HOP_SEARCH.selectWithSort");
			if(hopSearchList!=null)request.setAttribute("hopsearchlist", hopSearchList);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取热门搜索
	 * @param request
	 */
	public static List returnHopSearchList(){
		List hopSearchList = null;//6热门搜索
		SqlMapClient sqlmap=CSqlMap.getSqlMapConfig();
		try{
			hopSearchList = sqlmap.queryForList("T_HOP_SEARCH.selectWithSort");
		}catch(Exception e){
			e.printStackTrace();
		}
		return hopSearchList;
	}
}
